import 'package:flutter/material.dart';

class MyList extends StatefulWidget {
  MyList({Key key}) : super(key: key);

  @override
  _MyListState createState() => _MyListState();
}

class _MyListState extends State<MyList> {
  @override
  Widget build(BuildContext context) {
    return Padding(
        child: Container(
          constraints: BoxConstraints.expand(),
          decoration: BoxDecoration(
            color: Color.fromRGBO(250, 245, 223, 1),
            borderRadius: BorderRadius.all(Radius.circular(20)),
          ),
          child: Stack(
            alignment: Alignment.topCenter,
            children: <Widget>[
              Container(
                height: 60,
                alignment: Alignment.center,
                child: Text("My List"),
                decoration: BoxDecoration(
                  color: Color.fromRGBO(239, 232, 203, 1),
                  borderRadius: BorderRadius.vertical(top: Radius.circular(20)),
                ),
              ),
              Positioned(
                top: 70,
                left: 10,
                right: 10,
                bottom: 10,
                child: show_List(),
              )
            ],
          ),
        ),
        padding: EdgeInsets.fromLTRB(20, 10, 20, 10));
  }
}

List MyCardList = [
  {
    "category": '旅游',
    "spot": [
      {"name": '地点1', "state": true},
      {"name": '地点2', "state": true},
      {"name": '地点3', "state": false},
      {"name": '地点4', "state": false},
      {"name": '地点5', "state": true},
    ]
  },
  {
    "category": '吃喝',
    "spot": [
      {"name": '地点1', "state": true},
      {"name": '地点2', "state": true},
      {"name": '地点3', "state": false},
      {"name": '地点4', "state": false},
      {"name": '地点5', "state": true},
    ]
  },
  {
    "category": '玩乐',
    "spot": [
      {"name": '地点1', "state": true},
      {"name": '地点2', "state": true},
      {"name": '地点3', "state": false},
      {"name": '地点4', "state": false},
      {"name": '地点5', "state": true},
    ]
  },
  {
    "category": '电竞',
    "spot": [
      {"name": '地点1', "state": true},
      {"name": '地点2', "state": true},
      {"name": '地点3', "state": false},
      {"name": '地点4', "state": false},
      {"name": '地点5', "state": true},
    ]
  },
];

class show_List extends StatefulWidget {
  show_List({Key key}) : super(key: key);

  @override
  _show_ListState createState() => _show_ListState();
}

class _show_ListState extends State<show_List> {
  List<Widget> _getSpot(int index) {
    List<Widget> templist = []; //一定要初始化成空
    MyCardList[index]["spot"].forEach((spot) {
      // print(value);
      templist.add(CheckboxListTile(
        controlAffinity: ListTileControlAffinity.leading,
        value: spot["state"],
        title: Text(spot["name"]),
        onChanged: (v) {
          setState(() {
            spot["state"] = v;
          });
        },
        activeColor: Colors.green,
      ));
    });
    return templist.toList();
  }

  Widget _getMyList(context, index) {
    return ExpansionTile(
      title: Text(MyCardList[index]["category"]),
      children: this._getSpot(index),
    );
  }

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
      itemCount: MyCardList.length,
      itemBuilder: _getMyList,
    );
  }
}
